<?php
    require "./08-封装数据库完善版.php";
    $str = [
        'host'=>'127.0.0.1',
        "root"=>"root",
        "pwd"=>"root",
        "dbname"=>"db"
    ];
    $stu = new Db($str);
    // // var_dump($stu);
    // // 计算总记录数
    $sql="select count(*) from products";
    $total=$stu->select($sql)[0][0];
    // echo $total;

    // 每页数
    $size = 10;

    // 求出总页数
    $totalPage=ceil($total/$size);
    // 获取当前页码的数据
    $currentPage = $_GET['currentPage']??1;
    $currentPage = $currentPage<1?1:$currentPage;
    $currentPage = $currentPage>$totalPage?$totalPage:$currentPage;
    $start = ($currentPage-1)*$size;   
    $sql = "select * from products limit $start , $size";
    $data = $stu->select($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table{
            width: 780px;
            border: 1px solid #000;
            color: red;
            font-size: 18px;
        }
        td,th{
            border: 1px solid #000;
            width: 30px;
            height: 30px;
        }
        a{
            text-decoration: no;
        }
    </style>
</head>
<body>
    <table>
        <tr>
            <th>编号</th>
            <th>商品名称</th>
            <th>规格</th>
            <th>价格</th>
        </tr>
        
        <?php foreach($data as $item): ?>
        <tr>
            <td><?= $item[0] ?></td>
            <td><?= $item[1] ?></td>
            <td><?= $item[2] ?></td>
            <td><?= $item[3] ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
    <!-- 循环显示页码 -->
    <a href="./09-分页.php?currentPage=1">首页</a>
    <a href="./09-分页.php?currentPage=<?=$currentPage-1?>">上一页</a>
    <?php for($i=1;$i<=$totalPage;$i++): ?>
        <a href="./09-分页.php?currentPage=<?=$i?>"><?=$i?></a>
    <?php endfor ?>
    <a href="./09-分页.php?currentPage=<?=$currentPage+1?>">下一页</a>
    <a href="./09-分页.php?currentPage=<?=$totalPage?>">尾页</a>
</body>
</html>